﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ZThing
{
    public partial class Form3 : Form
    {
        string mapname;
        int X=0, Y=0;
        string[] tileset;
        PictureBox cPB;
        PictureBox[][] PBT = { new PictureBox[100], new PictureBox[100]};
        public Form3(string map)
        {
            InitializeComponent();
            mapname = map;
        }
        List<string> idcka = new List<string>();
        Random rnd = new Random();
        PictureBox[] pb = new PictureBox[100];
        PictureBox[,] pb2D = new PictureBox[10, 10];
        private void Form3_Load(object sender, EventArgs e)
        {
            this.BackgroundImage = Image.FromFile("textures/000.png");
            this.BackgroundImageLayout = ImageLayout.Tile;
            tileset = File.ReadAllLines("textures.id");
            for (int line = 0; line < tileset.Length; line++)
            {
                string[] list = tileset[line].Split(' ');
                listBox1.Items.Add(list[1]);
                idcka.Add(list[0]);
            }
            int pbC = 0;
            string[] tiles = File.ReadAllLines(mapname);
            string[] map = { };
            

            for (int y = 0; y < 10; y++)
            {
                try { map = tiles[y].Split(' '); }
                catch { MessageBox.Show("Map is corrupted![0]"); Application.ExitThread(); break; }
                for (int x = 0; x < 10; x++)
                {
                    pb[pbC] = new PictureBox();
                    pb[pbC].Location = new Point(X, Y);
                    pb[pbC].Size = new Size(64, 64);
                    pb[pbC].BackColor = Color.Transparent;
                    pb[pbC].BorderStyle = BorderStyle.FixedSingle;
                    pb[pbC].BackgroundImage = Image.FromFile(@"textures/000.png");
                    pb[pbC].Tag = "000.png";
                    pb[pbC].MouseClick += PBox;
                    switch (map[x])
                    {
                        case "0":
                            int rand = rnd.Next(0, 4);
                            if (rand == 0) pb[pbC].BackgroundImage = Image.FromFile(@"textures\000.png");
                            else pb[pbC].BackgroundImage = Image.FromFile(@"textures\000a.png"); ;
                            break;
                        default:
                            try
                            {
                                string[] id = {};
                                for (int ai = 0; ai < tileset.Length; ai++)
                                {
                                    id = tileset[ai].Split(' ');
                                    if (map[x] == id[0])
                                    {
                                        pb[pbC].BackgroundImage = Image.FromFile(@"textures/" + id[1].ToString());
                                        pb[pbC].Tag = id[1];
                                        //break;
                                    }

                                }
                            }
                            catch (Exception ex)
                            {

                                MessageBox.Show("Map is incorrectly defined! Syntax error at tile: " + y + ":" + x);
                                Application.ExitThread();
                            }
                            break;
                    }
                    this.Controls.Add(pb[pbC]);
                    pb2D[y,x] = pb[pbC];
                    X += 64;
                    pbC++;
                }
                X = 0;
                Y += 64;
            }
        }

        void PBox(object sender, MouseEventArgs e)
        {
            PictureBox tmp = (PictureBox)sender;
            pictureBox1.Image = tmp.Image;
            string[] tile = { };
            string texture = "";
            for (int i = 0; i < tileset.Length; i++)
            {
                
                tile = tileset[i].Split(' ');
                if (tmp.Tag.ToString() == tile[1]) texture = tile[0];
                
            }
            try { textBox1.Text = texture; }
            catch { textBox1.Text = ""; }
            cPB = tmp;
            try
            {
                if (listBox1.SelectedItem.ToString() != "")
                {
                    tmp.BackgroundImage = Image.FromFile("textures/" + listBox1.SelectedItem.ToString());
                    tmp.Tag = listBox1.SelectedItem.ToString();
                }
            }
            catch { }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if(textBox1.Text != "")
            {
                string[] tile = {};
                string texture = "";
                for (int i = 0; i < tileset.Length; i++)
                {
                    tile = tileset[i].Split(' ');
                    if (textBox1.Text == tile[0]) texture = tile[1];
                }
                try { pictureBox1.Image = Image.FromFile(@"textures/" + texture); }
                catch { pictureBox1.BackColor = Color.White; }
            }
        }

        private void PBox_click(object sender, MouseEventArgs e)
        {


        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (textBox1.Text != "")
            {
                if(e.KeyCode == Keys.Return)
                {
                string[] tile = { };
                string texture = "";
                for (int i = 0; i < tileset.Length; i++)
                {
                    tile = tileset[i].Split(' ');
                    if (textBox1.Text == tile[0]) texture = tile[1];
                }
                try { pictureBox1.Image = Image.FromFile(@"textures/" + texture); cPB.BackgroundImage = Image.FromFile(@"textures/" + texture); cPB.Tag = texture; }
                catch { pictureBox1.BackColor = Color.White; }
            }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] buff = new string[10];
            int count = 0;
            for (int x = 0; x < 10; x++)
            {
                for (int y = 0; y < 10; y++)
                {
                    string[] tileset = File.ReadAllLines("textures.id");
                    string tid = "";
                    for (int i = 0; i < tileset.Length; i++)
			        {
			            string[] id = tileset[i].Split(' ');
                        if(id[1]==pb[count].Tag.ToString())
                            tid=id[0];
			        }
                    if(tid=="")
                    {
                        MessageBox.Show("Error during texture processing...");
                        Application.ExitThread();
                    }
                    buff[x] += tid + " ";
                    count++;
                }
            }
            File.Delete(mapname);
            File.WriteAllLines(mapname, buff);
            MessageBox.Show("Map saved.");
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                pictureBox1.Image = Image.FromFile(@"textures/" + listBox1.SelectedItem.ToString());
                textBox1.Text = idcka[listBox1.SelectedIndex];
            }
            catch {  }
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                string[] tile = { };
                string texture = "";
                for (int i = 0; i < tileset.Length; i++)
                {
                    tile = tileset[i].Split(' ');
                    if (idcka[listBox1.SelectedIndex] == tile[0]) texture = tile[1];
                }
                try { cPB.BackgroundImage = Image.FromFile(@"textures/" + texture); cPB.Tag = texture; }
                catch { pictureBox1.BackColor = Color.White; }
            }
            catch { }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.SelectedIndex = -1;
        }
    }
}
